package cn.yunhe.controller;

import cn.yunhe.entity.Coll;
import cn.yunhe.services.CollStuServices;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2018/5/28.
 */
@Controller
@RequestMapping("/coll")
public class CollController{
    @Resource(name = "collStuServicesImpl")
    public CollStuServices collStuServices;

    @RequestMapping("/allColl")
    public ModelAndView allColl(){
        List<Coll> list = collStuServices.queryAllColl(1);
        System.out.println(list.size());
        for (Coll cl:list){
            System.out.println(cl.toString());
        }
        System.out.println(list.size());
        ModelAndView mv = new ModelAndView("ftl/stu_coll");
        mv.addObject("list",list);
        return mv;
    }
/*
    @RequestMapping("/toupdateColl")
    public String toUpdateColl(Model model,int id){
       model.addAttribute("coll",collStuServices.queryById(id));
        return "updateColl";

    }
    @RequestMapping("/updateColl")
    public  String updateColl(Model model,Coll coll){
        collStuServices.updateColl(coll);
        coll = collStuServices.queryById(coll.getColl_id());
        model.addAttribute("coll",coll);
        return "redirect:ftl/stu_coll";
    }*/

    @RequestMapping("/update")
    public String update(Model model,Coll coll){
        if (collStuServices.updateColl(coll)){
             coll=collStuServices.queryById(coll.getColl_id());
            model.addAttribute("coll",coll);
            return "redirect:/coll/allColl.do";
        }else {
            return "/error";
        }

    }

    @RequestMapping("/delete")
    public String delete(int id){
        System.out.println(id);
        collStuServices.deleteById(id);
        return "redirect:/coll/allColl.do";
    }

    @RequestMapping("/collInfo")
    public String getUsers(@RequestParam(value ="pn",defaultValue = "1")Integer pn, Model model){
        //从第一条开始 每页查询五条数据
        PageHelper.startPage(pn, 5);
        List<Coll> users = collStuServices.queryAllColl(1);
        //将用户信息放入PageInfo对象里
        PageInfo page = new PageInfo(users,5);
        model.addAttribute("pageInfo", page);
        return "redirect:/coll/allColl.do";
    }

    public CollStuServices getCollStuServices() {
        return collStuServices;
    }

    public void setCollStuServices(CollStuServices collStuServices) {
        this.collStuServices = collStuServices;
    }
}
